package com.tim.wicket.examples;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.wicket.Application;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;

import com.tim.wicket.examples.model.Cheese;
import com.tim.wicket.examples.util.CheesePropertyReader;

/**
 * Application object for your web application. If you want to run this application without deploying, run the Start class.
 * 
 * @see com.tim.wicket.examples.Start#main(String[])
 */
public class CheesrApplication extends WebApplication
{    	
	
	private List<Cheese> cheeses = Collections.unmodifiableList(readCheeses());
	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	@Override
	public Class<Index> getHomePage()
	{
		return Index.class;
	}

	private List<? extends Cheese> readCheeses() {
		List<Cheese> cheeses = new ArrayList<Cheese>();
		
		try {
			CheesePropertyReader reader = new CheesePropertyReader(CheesrApplication.class.getResourceAsStream("cheeses.properties"));
			cheeses = reader.readCheeses();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return cheeses;
	}

	/**
	 * @see org.apache.wicket.Application#init()
	 */
	@Override
	public void init()
	{
		super.init();
		// add your configuration here
	}
	
	public static CheesrApplication get() {
		return (CheesrApplication) Application.get();
	}
	
	public List<Cheese> getCheeses() {
		return Collections.unmodifiableList(cheeses);
	}
	
	@Override
	public Session newSession(Request request, Response response) {
		return new CheesrSession(request);
	}
	
}
